// pages/secondlist/secondlist.js
let db = wx.cloud.database()
let subject = db.collection("subject")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        allData : [],
        ind : 0,
        iscollect : false,
    },
    getDetailData(id,keyword,type,search,level,classify,mine){
        // console.log(subject)
        //判断传过来的值，选择执行方法
        if(keyword == 'hot'){
            this.getHot(id);
        }else if(keyword == 'type'){
                // console.log(type)
                // console.log(classify);
                if(classify == 'true'){
                  this.getLevelList(id,type,level)
                }else{
                  this.getTypeList(id,type)
                }
        }else if(search){
            // console.log(id,search);
            this.getSearchList(id,search);
        }else if(mine){
            if(mine == 'collect'){
              this.getCollect(id);
            }else if(mine == 'history'){
              this.getHistory(id);
            }
        }
    },
    // 获得热门列表
    getHot(ID){
        let that = this;
        subject.where({
            hot : true
        }).get().then(
            res => {
                if(res.errMsg == "collection.get:ok"){
                //   console.log(res.data);
                  that.setData({
                    allData: res.data
                  })
                //   console.log(that.data.allData)
                  that.getIndex(that.data.allData,ID)
                }
              }
        )
    },
    // 获得具体类型列表
    getTypeList(id,type){
        let that = this;
        subject.where({
            type : type
        }).get().then(
            res => {
                if(res.errMsg == "collection.get:ok"){
                //   console.log(res.data);
                  that.setData({
                    allData : res.data
                  })
                  that.getIndex(that.data.allData,id)
                  wx.hideLoading();
                }
              }
        )
    },
    // 获得等级列表
    getLevelList(id,type,level){
      let that = this;
      subject.where({
          type : type,
          level : level
      }).get().then(
          res => {
              if(res.errMsg == "collection.get:ok"){
              //   console.log(res.data);
                that.setData({
                  allData : res.data
                })
                that.getIndex(that.data.allData,id)
                wx.hideLoading();
              }
            }
      )
  },
    getIndex(arr,id){
        if(arr.length>0){
            // console.log(arr)
            arr.forEach((element,index) => {
            //    console.log(element._id) 
                if(element._id == id){
                    this.setData({
                        ind : index
                    })
                    this.isCollectFun();
                    if(wx.getStorageSync('isShow')){
                        this.recordFun();
                    }
                }
            });
            wx.hideLoading()
        }
    },
    // 获得搜索列表
    getSearchList(id,v){
        // console.log(v);
        let typeList = [];
        let questionList = [];
        let answerList = [];
        let levelList = [];
        let resultData = [];
        let that = this;
        // 类型
        subject.where({
            type: db.RegExp({
              regexp: v,
              options: 'i',
            })
          }).get().then(
            res => {
                if(res.errMsg == "collection.get:ok"){
                    typeList  = res.data
                    // console.log(typeListList);
                    resultData =  resultData.concat(typeList);
                    // 问题
                    subject.where({
                      question: db.RegExp({
                        regexp: v,
                        options: 'i',
                      })
                    }).get().then(
                      res => {
                          if(res.errMsg == "collection.get:ok"){
                              questionList  = res.data
                              // console.log(questionList);
                              resultData = resultData.concat(questionList);
                              // 答案
                              subject.where({
                                answer: db.RegExp({
                                  regexp: v,
                                  options: 'i',
                                })
                              }).get().then(
                                res => {
                                    if(res.errMsg == "collection.get:ok"){
                                        answerList  = res.data
                                        // console.log(answerList);
                                        resultData = resultData.concat(answerList);
                                        // 等级
                                        subject.where({
                                          level: db.RegExp({
                                            regexp: v,
                                            options: 'i',
                                          })
                                        }).get().then(
                                          res => {
                                              if(res.errMsg == "collection.get:ok"){
                                                  levelList  = res.data
                                                  // console.log(levelList);
                                                  resultData = resultData.concat(levelList);
                                                      // console.log(resultData);
                                                      resultData = that.removeSame(resultData);
                                                      that.setData({
                                                        allData : resultData
                                                      })
                                                      that.getIndex(that.data.allData,id)
                                                      wx.hideLoading();
                                              }
                                            }
                                      )
                                    }
                                  }
                            )
                          }
                        }
                  )
                }
              }
        )        
    },
    // 数据去重
    removeSame(arr){   
      let newarr = [];
      arr.forEach(element => {
        let isSame = true;
        for(let i = 0 ; i<newarr.length ; i++){
            if(element._id == newarr[i]._id){
              isSame = false;
            }
        }
        if(isSame){
          newarr.push(element);
        }
        });
        // console.log(newarr);
        return newarr;
    },
    // 上一题
    prev(e){
        // console.log(this.data.ind);
        // console.log(this.data.allData);
        if(this.data.ind <=0){
            wx.showToast({
              icon: 'error',
              title: '已经是第一题了',
            })
        }else{
            let num = this.data.ind - 1;
            this.setData({
                ind : num
            })
            this.isCollectFun();
            if(wx.getStorageSync('isShow')){
              this.recordFun();
          }
        }
    },
    // 判断是否收藏
    isCollectFun(){
      let iscollect = this.data.allData[this.data.ind].collect;
      if(wx.getStorageSync('isShow')){
        this.setData({
          iscollect : iscollect
        })
      }
    },
    // 下一题
    next(e){
        // console.log(this.data.ind);
        // console.log(this.data.allData);
        if(this.data.ind >= this.data.allData.length-1){
            wx.showToast({
              icon: 'error',
              title: '已经是最后一题了',
            })
        }else{
            let num = this.data.ind + 1;
            this.setData({
                ind : num
            })
            this.isCollectFun();
            if(wx.getStorageSync('isShow')){
              this.recordFun();
            }
        }
    },
    // 收藏
    collectButton(){
      if(wx.getStorageSync('isShow')){
      let that = this;
      let id = this.data.allData[this.data.ind]._id;
      // console.log(id)
      subject.doc(id).update({
        data:{
          collect :true
        }
      }).then(
        res=>{
          // console.log(res);
          if(res.errMsg == "document.update:ok"){
            let newarr = that.data.allData;
            newarr[this.data.ind].collect = true;
            that.setData({
              iscollect : true,
              allData : newarr,
            })
            wx.showToast({
              title: '收藏成功',
            })
          }
        }
      )
      }else{
        wx.showToast({
          icon : 'error',
          title: '请先登录',
        })
      }
    },
    // 取消收藏
    cancelButton(){
      let that = this;
      let id = this.data.allData[this.data.ind]._id;
      // console.log(id)
      subject.doc(id).update({
        data:{
          collect :false
        }
      }).then(
        res=>{
          // console.log(res);
          if(res.errMsg == "document.update:ok"){
            let newarr = that.data.allData;
            newarr[this.data.ind].collect = false;
            that.setData({
              iscollect : false,
              allData : newarr,
            })
            wx.showToast({
              title: '取消收藏',
            })
          }
        }
      )
    },
    // 历史记录
    recordFun(){
      let id = this.data.allData[this.data.ind]._id;
      subject.doc(id).update({
        data:{
          record : true
        }
      }).then(
        res =>{
          if(res.errMsg == "document.update:ok"){
            // console.log('记录')
          }
        }
      )
    },
    // 获取历史记录
    getHistory(ID){
      let that = this;
        subject.where({
            record : true
        }).get().then(
            res => {
                if(res.errMsg == "collection.get:ok"){
                //   console.log(res.data);
                  that.setData({
                    allData: res.data
                  })
                //   console.log(that.data.allData)
                  that.getIndex(that.data.allData,ID)
                }
              }
        )
    },
    // 获取收藏列表
    getCollect(ID){
      let that = this;
        subject.where({
            collect : true
        }).get().then(
            res => {
                if(res.errMsg == "collection.get:ok"){
                //   console.log(res.data);
                  that.setData({
                    allData: res.data
                  })
                //   console.log(that.data.allData)
                  that.getIndex(that.data.allData,ID)
                }
              }
        )
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options){
        // console.log(options.id,options.keyword,options.searchword,options.level,options.mine);
        wx.showLoading({
            title: '玩命加载中',
          })
        this.getDetailData(options.id,options.keyword,options.type,options.searchword,options.level,options.classify,options.mine);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})